package com.nf.demo.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.stream.Stream;

/**
 * @author lwj
 * @date 2025-02-24
 * @vesion 1.0.0
 */
public class DBUtils {
    // 连接对象
    private static Connection connection;
    // jdbc 连接信息
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    static{
        try{
            // 加载配置文件信息
            Properties properties = new Properties();
            InputStream input = DBUtils.class.getClassLoader().getResourceAsStream("db.properties");
            properties.load(input);

            // jdbc 连接信息
            driver = properties.getProperty("db.driver");
            url = properties.getProperty("db.url");
            username = properties.getProperty("db.username");
            password = properties.getProperty("db.password");

            // 加载驱动
            Class.forName(driver);
        } catch (IOException  | ClassNotFoundException e) {
            System.out.println("加载驱动失败！");
            throw new RuntimeException(e);
        }
    }

    /**
     * 开启数据库连接
     * @return 返回连接对象
     */
    public static Connection getConnection(){
        try {
            connection = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            System.out.println("开启连接失败！");
            throw new RuntimeException(e);
        }
        return connection;
    }

    /**
     * 获取结果集对象
     * @param sql SQL语句
     * @param params 注入参数数组
     * @return 返回结果集对象
     */
    public static ResultSet executeQuery(String sql, ArrayList<Object> params){
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        try{
            preparedStatement = connection.prepareStatement(sql);
            if (!params.isEmpty()){
                // 注入参数
                for (int i = 0; i < params.size(); i++) {
                    preparedStatement.setObject(i+1,params.get(i));
                }
            }
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            System.out.println("返回结果集对象失败！");
            throw new RuntimeException(e);
        } finally {
            // closeConnection(connection,preparedStatement,resultSet);
        }
        return resultSet;
    }

    /**
     * 获取受影响的行数
     * @param sql SQL语句
     * @param params 注入参数数组
     * @return 返回受影响的行数
     */
    public static Integer executeUpdate(String sql, ArrayList<Object> params){
        Integer count = -1;
        PreparedStatement preparedStatement = null;
        try{
            preparedStatement = connection.prepareStatement(sql);
            if (!params.isEmpty()){
                // 注入参数
                for (int i = 0; i < params.size(); i++) {
                    preparedStatement.setObject(i+1,params.get(i));
                }
            }
            count = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            System.out.println("返回受影响的行数失败！");
            throw new RuntimeException(e);
        } finally {
            // closeConnection(connection,preparedStatement,null);
        }
        return count;
    }

    /**
     * 获取自增编号ID
     * @param sql SQL语句
     * @param params 注入参数数组
     * @return 返回自增编号
     */
    public static Integer executeQueryAddId(String sql, ArrayList<Object> params){
        Integer count = -1;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        try{
            preparedStatement = connection.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            if (!params.isEmpty()){
                // 注入参数
                for (int i = 0; i < params.size(); i++) {
                    preparedStatement.setObject(i+1,params.get(i));
                }
            }
            preparedStatement.executeUpdate();
            // 获取自增主键
            resultSet = preparedStatement.getGeneratedKeys();
            if(resultSet.next()){
                count = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            System.out.println("返回自增编号失败！");
            throw new RuntimeException(e);
        } finally {
            // closeConnection(connection,preparedStatement,resultSet);
        }
        return count;
    }

    /**
     * 关闭资源
     * @param connection 连接对象
     * @param statement 预处理对象
     * @param resultSet 结果集对象
     */
    public static void closeConnection(Connection connection, Statement statement, ResultSet resultSet){
        try {
            if (resultSet != null){
                resultSet.close();
            }
        } catch (SQLException e) {
            System.out.println("关闭结果集对象失败！");
            throw new RuntimeException(e);
        }
        try{
            if (statement != null){
                statement.close();
            }
        } catch (SQLException e) {
            System.out.println("关闭预处理对象失败！");
            throw new RuntimeException(e);
        }
        try {
            if (connection != null){
                connection.close();
            }
        } catch (SQLException e) {
            System.out.println("关闭连接对象失败！");
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        Connection connection1 = getConnection();
        System.out.println(connection1);
    }
}